package com.zhenti.z1;

import java.util.Arrays;

public class magicStone {

    public static int minConst(int [][] stones){
        int n  = stones.length;
        Arrays.sort(stones,(a,b) -> a[0] == 0 && b[0] == 0 ?  (b[1]-b[2] - a[1]+a[2]): (a[0] - b[0]));
        int zero = 0;
        int red = 0;
        int blue = 0;
        int cost = 0;
        for (int i = 0; i < n; i++) {
            if (stones[i][0] == 0){
                zero++;
                cost +=stones[i][1];
            }else if (stones[i][0] == 1){
                red++;
            }else {
                blue++;
            }
        }
        if (red>(n>>1) || blue >(n>>1))
            return -1;
        blue = zero -(n>>1 -red);
        for (int i = 0; i < blue; i++) {
            cost += stones[i][2] - stones[i][1];
        }
        return  cost;
    }

    public static void main(String[] args) {
        int stones[][] ={{0,4,6},{0,5,8},{0,4,6},{0,5,8},{0,4,6},{0,5,8}};
        System.out.println(minConst(stones));



    }
}
